﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ClusterStatusActioner.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The cluster status actioner.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Actioners.Activities
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using NHibernate;

    using ServiceLibrary.Models.Domain;
    using ServiceLibrary.Services.Commands;
    using ServiceLibrary.Services.Queries;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The cluster status actioner.
    /// </summary>
    public class ClusterStatusActioner : ActionerBase
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ClusterStatusActioner"/> class.
        /// </summary>
        /// <param name="session">
        /// The session.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public ClusterStatusActioner(ISession session, User activeUser)
            : base(session, activeUser)
        {
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The create cluster status.
        /// </summary>
        /// <param name="method">
        /// The method.
        /// </param>
        /// <returns>
        /// The <see cref="Guid"/>.
        /// </returns>
        public Guid CreateClusterStatus(ClusterStatus method)
        {
            this.Session.Save(method);

            return method.ID;
        }

        /// <summary>
        /// The create cluster status.
        /// </summary>
        /// <param name="cmd">
        /// The cmd.
        /// </param>
        public void CreateClusterStatus(CreateClusterStatusCommand cmd)
        {
            if (cmd.Status != null)
            {
                var status = new ClusterStatus
                                 {
                                     ID = cmd.Status.ID, 
                                     Comments = cmd.Status.Comments, 
                                     IsActive = cmd.Status.IsActive, 
                                     Type = this.Session.Get<ClusterStatusType>(cmd.Status.Type)
                                 };

                CreateClusterStatus(status);
            }
        }

        /// <summary>
        /// The create cluster status type.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        public void CreateClusterStatusType(string name)
        {
            var type = new ClusterStatusType
                           {
                               ID = Guid.NewGuid(), 
                               CreatedDateTime = DateTime.Now, 
                               Name = name
                           };
            this.Session.Save(type);
        }

        /// <summary>
        /// The execute search query.
        /// </summary>
        /// <param name="clusterMethodQuery">
        /// The cluster method query.
        /// </param>
        /// <returns>
        /// The <see cref="ClusterStatusResult"/>.
        /// </returns>
        public ClusterStatusResult ExecuteSearchQuery(ClusterStatusQuery clusterMethodQuery)
        {
            SearchSettings settings = clusterMethodQuery.Settings;
            var result = new ClusterStatusResult();

            IQueryOver<ClusterStatus, ClusterStatus> search = this.Session.QueryOver<ClusterStatus>();
            this.ApplyDefaultCriterias(clusterMethodQuery.Settings, search);

            result.TotalRowCount = search.RowCount();
            IList<ClusterStatus> list = ApplySkipAndTake(settings, search);

            if (list.Any())
            {
                list = list.OrderBy(x => x.Type.Name).ToList();
                foreach (ClusterStatus status in list)
                {
                    result.List.Add(Services.Dto.ClusterStatus.Convert(status));
                }
            }

            return result;
        }

        /// <summary>
        /// The query all cluster status types.
        /// </summary>
        /// <param name="clusterStatusTypesQuery">
        /// The cluster status types query.
        /// </param>
        /// <returns>
        /// The <see cref="ClusterStatusTypeResult"/>.
        /// </returns>
        public ClusterStatusTypeResult QueryAllClusterStatusTypes(ClusterStatusTypeQuery clusterStatusTypesQuery)
        {
            var result = new ClusterStatusTypeResult();

            var list =
                this.Session.QueryOver<ClusterStatusType>().List().OrderBy(x => x.Name);
            
            result.TotalRowCount = list.Count();

            foreach (ClusterStatusType type in list)
            {
                result.List.Add(Services.Dto.ClusterStatusType.Convert(type));
            }

            return result;
        }

        #endregion

        #region Methods

        /// <summary>
        /// The delete cluster status.
        /// </summary>
        /// <param name="cmd">
        /// The cmd.
        /// </param>
        internal void DeleteClusterStatus(DeleteClusterStatusCommand cmd)
        {
            if (cmd.Dto != Guid.Empty)
            {
                var status = this.Session.Get<ClusterStatus>(cmd.Dto);
                if (status != null)
                {
                    this.Session.Delete(status);
                }
            }
        }

        /// <summary>
        /// The update cluster status.
        /// </summary>
        /// <param name="cmd">
        /// The cmd.
        /// </param>
        internal void UpdateClusterStatus(UpdateClusterStatusCommand cmd)
        {
            if (cmd.Dto != null)
            {
                var status = this.Session.Get<ClusterStatus>(cmd.Dto.ID);
                if (status != null)
                {
                    status.Comments = cmd.Dto.Comments;
                    status.IsActive = cmd.Dto.IsActive;
                    status.Type = this.Session.Get<ClusterStatusType>(cmd.Dto.Type);
                    this.Session.Update(status);
                }
            }
        }

        #endregion
    }
}